<?php

/*
 *
 */

include 'seesmic/date.php'; // seesmic date functions

session_start();

if (!$_GET['page']) {
	$page = 1;
	$offset = 0;
} else {
	$page = $_GET['page'];
	$offset = ($page - 1) * $_SESSION['squantity'];
}

$tagsearch = addslashes($_GET['tagsearch']);

$prev = $page - 1;
$next = $page + 1;

echo "<div id='nav'>";

// display current position
echo "Page ".$page;

include('../../../seesmicwsdbinfo-live.php');

// connect to the database
$wslink = mysql_connect($wsdbhost, $wsdbuser, $wsdbpass);
if (!$wslink) {
	die('Problem connecting to database server : ' . mysql_error());
}

// make $dbname the current db
$wsdb_selected = mysql_select_db($wsdbname, $wslink);
if (!$wsdb_selected) {
	die ('Problem connecting to database : ' . mysql_error());
}

include('../../../sidebardbinfo-live.php');

// connect to the database
$link = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$link) {
	die('Problem connecting to database server : ' . mysql_error());
}

// make $dbname the current db
$db_selected = mysql_select_db($dbname, $link);
if (!$db_selected) {
	die ('Problem connecting to database : ' . mysql_error());
}

$countstmt = "SELECT COUNT(*) FROM videos v JOIN tags_lookup tl ON (v.video_id=tl.video_id) JOIN tags_keywords k ON (tl.word_id=k.word_id) WHERE k.word='".$tagsearch."'";

$countres = mysql_query($countstmt);
if (!$countres) {
	die ('Problem with query : ' . mysql_error());
}

$num_posts = mysql_result($countres,0);

$stmt = "SELECT v.* FROM videos v JOIN tags_lookup tl ON (v.video_id=tl.video_id) JOIN tags_keywords k ON (tl.word_id=k.word_id) WHERE k.word='".$tagsearch."' ORDER BY v.created_at DESC LIMIT ".$offset.",".$_SESSION['squantity'];

$res = mysql_query($stmt);
if (!$res) {
	die ('Problem with query : ' . mysql_error());
}

$currentstart = $offset + 1;
$currentend   = $offset + $_SESSION['squantity'];

// display previous button if appropriate
if ($prev >= 1) {
	echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:showTagVideos('".$tagsearch."',".$prev.");\">&lt;</a>&nbsp;</span>";
} else {
	echo "&nbsp;&nbsp;&nbsp;&nbsp;";
}

// display record count and current position in record set
echo $currentstart." to ";
if ($num_posts > $currentend) {
	echo $currentend." of ";
} else {
	echo $num_posts." of ";
}
echo $num_posts;

// display next button if appropriate
if ($next <= (($num_posts / $_SESSION['squantity']))+1) {
	echo "&nbsp;<a href=\"javascript:showTagVideos('".$tagsearch."',".$next.");\">&gt;</a>";
}

echo "</div>";

$seesmic = "\n<div class=\"scroll\">\n";
$seesmic .= "\t<table id=\"sees_table\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";

$thread_counter_array = array();

while ($row = mysql_fetch_assoc($res)) {

	$video_id 			= $row['video_id'];
	$source_server 		= $row['source_server'];
	$thread_posts		= $row['thread_posts'];
	$replies			= $thread_posts - 1;
	$title				= stripslashes($row['title']);
	$language			= $row['language'];
	$username			= $row['username'];
	$url_flv			= $row['url_flv'];
	if (substr($url_flv,0,23) == "http://fms3.seesmic.com") {
		$ending = substr($url_flv,23,strlen($url_flv));
		$url_flv = "http://v.seesmic.com".$ending;
	}
	$thread_id			= $row['thread_id'];
	$reply_to_video_id	= $row['reply_to_video_id'];
	$source				= $row['source'];
	$reply_to_username	= $row['reply_to_username'];
	$url_mp4			= $row['url_mp4'];
	$url_thumbnail		= $row['url_thumbnail'];
	if (substr($url_thumbnail,0,23) == "http://fms3.seesmic.com") {
		$ending = substr($url_thumbnail,23,strlen($url_thumbnail));
		$url_thumbnail = "http://t.seesmic.com".$ending;
	}
	$created_at			= parse_datetime($row['created_at']);
	$url_player			= $row['url_player'];

	if (!array_key_exists($thread_id,$thread_counter_array)) {
		$threadcountstmt = "SELECT thread_posts FROM videos WHERE thread_id = '".$thread_id."' ORDER BY created_at DESC LIMIT 0,1";

		$threadcountres = mysql_query($threadcountstmt);
		if (!$threadcountres) {
			die ('Problem with query : ' . mysql_error());
		}

		$threadcount = mysql_result($threadcountres,0);
		$thread_counter_array[$thread_id] = $threadcount - 1;
	}

		$thumbnail_cache_uri = "../images/thumbnails/".$video_id.".jpg";

		$thumbnail_cache_url = "images/thumbnails/".$video_id.".jpg";

		if (file_exists($thumbnail_cache_uri)) {
			$thumbnail_cache_url = "images/thumbnails/".$video_id.".jpg";
		} else {
			$thumbnail_cache_url = $url_thumbnail;
		}


	$seesmic .= "\t<tr>\n";
	$seesmic .= "\t<td align=\"middle\" width=\"82\" valign=\"top\" style=\"color:white;font-size: 75%;font-family: arial, 'lucida console', sans-serif;\">\n";
	$seesmic .="<a href=\"javascript:loadFlv('";
	$seesmic .= $url_flv;
	$seesmic .= "','";
	$seesmic .= $video_id;
	$seesmic .= "','";
	$seesmic .= addslashes($title);
	$seesmic .= "','";
	$seesmic .= $username;
	$seesmic .= "','";
	$seesmic .= $_SESSION['pwidth'];
	$seesmic .= "','";
	$seesmic .= $_SESSION['pheight'];
	$seesmic .= "','";
	$seesmic .= $_SESSION['sheight'];
	$seesmic .= "');\">";
	$seesmic .= "<img class=\"seesmic_img\" src=\"".$thumbnail_cache_url."\" width=\"80\" height=\"60\" style=\"border: 0px;\" alt=\"play video\" /></a>\n";

	$seesmic .= "\t<br />";

	if ($thread_counter_array[$thread_id] >= 2) {
		$seesmic .= "\t<a href=\"javascript:stopScroll();getThreadTimeline('".$video_id."',1,'yes');\">".$thread_counter_array[$thread_id]." replies&nbsp;&gt;&gt;</a>\n";
	} elseif ($thread_counter_array[$thread_id] == 1) {
		$seesmic .= "\t<a href=\"javascript:stopScroll();getThreadTimeline('".$video_id."',1,'yes');\">1 reply&nbsp;&gt;&gt;</a>\n";
	} else {
		$seesmic .= "\t0 replies\n";
	}

	$seesmic .= "\t</td>\n";
	$seesmic .= "\t<td valign=\"top\" valign=\"top\"><span style=\"color:white;font-size: 75%;font-family: arial, 'lucida console', sans-serif;\">";
	$seesmic .="<a href=\"javascript:loadFlv('";
	$seesmic .= $url_flv;
	$seesmic .= "','";
	$seesmic .= $video_id;
	$seesmic .= "','";
	$seesmic .= addslashes($title);
	$seesmic .= "','";
	$seesmic .= $username;
	$seesmic .= "','";
	$seesmic .= $_SESSION['pwidth'];
	$seesmic .= "','";
	$seesmic .= $_SESSION['pheight'];
	$seesmic .= "','";
	$seesmic .= $_SESSION['sheight'];
	$seesmic .= "');\">";
	$seesmic .= $title."</a></span>";
	$seesmic .= "\t<br /><span style=\"color:yellow;font-size: 75%;font-family: arial, 'lucida console', sans-serif\"><small>".$created_at."</small></span>\n";

	$wslinkstmt = "SELECT url FROM links WHERE video_id = '".$video_id."'";

	$wslinkres = mysql_query($wslinkstmt,$wslink);
	if (!$wslinkres) {
		die ('Problem with query : ' . mysql_error());
	}

	$wsrow = mysql_fetch_row($wslinkres);

	if ($wsrow) {
		$url = $wsrow[0];
		// display link
		$seesmic .= "<small><span style=\"color:green;font-size: 75%;font-family: arial, 'lucida console', sans-serif\"><a href=\"".$url."\" target=\"_blank\">link</a></span></small>\n";
	} else {
		// if is logged in user's video is showing then display add link
		if ($_SESSION['seesmicName'] == $username) {
			$seesmic .= "\t<span style=\"color:green;font-size: 75%;font-family: arial, 'lucida console', sans-serif\"><small><a href=\"javascript:getAddLink('".$video_id."','".$url_thumbnail."','".addslashes($title)."');\">add link</a></small></span>\n";
		}
	}

	// display profile icon
	$seesmic .= "\t<br /><a href=\"javascript:getUserProfile('".$username."');\"><img src=\"images/profile.png\" border=\"0\" /></a>";

	// display username
	$seesmic .= "\t<div id='username'><a href=\"javascript:stopScroll();getUserTimeline('".$username."',1,'yes');\">".$username."</a></div>";

	// display language
	$seesmic .= "<span style=\"color:pink;font-size: 75%;font-family: arial, 'lucida console', sans-serif\"><small>&nbsp;(".$language.")</small></span>\n";

	// display download flv link
	$seesmic .= "<small><a href=\"".$url_flv."\" style=\"color:red;font-size: 75%;font-family: arial, 'lucida console', sans-serif\">flv</a></small>&nbsp;";

	// display link to play in seesmic
	$seesmic .= "<div id='seesmic_link'><a href=\"http://www.seesmic.com/Seesmic.html#/video/".$video_id."/watch\" target=\"_new\">seesmic</a></div>\n";

	$seesmic .= "\t</td></tr>\n";
}
$seesmic .="</table></div>\n";
mysql_close($link);
mysql_close($wslink);
echo $seesmic;
?>